Constructing the database


In [2]:
from pgmpy.factors import TabularCPD

In [7]:
ez_cpd  = TabularCPD(variable = 'ez',
                        variable_card = 2,
                        values = [[0.99, 0.35, 0.15, 0.05],
                                  [0.01, 0.65, 0.85, 0.95]],
                        evidence= ['nb', 'ch'],
                        evidence_card=[2, 2])

b_cpd  = TabularCPD(variable = 'nb',
                        variable_card = 2,
                        values = [[0.5, 0.5]])

ch_cpd  = TabularCPD(variable = 'ch',
                        variable_card = 2,
                        values = [[0.5, 0.5]])

In [8]:
from pgmpy.models import BayesianModel

model = BayesianModel([('nb', 'ez'), ('ch', 'ez')])
model.add_cpds(ez_cpd, b_cpd, ch_cpd)

In [9]:
from pgmpy.inference import VariableElimination
inf = VariableElimination(model)

In [12]:
%%timeit -n 1 -r 1
p_b = inf.query(variables=['nb'], evidence={'ez': 1, 'ch': 0})
print(p_b['nb'])


╒══════╤═══════════╕
│ nb   │   phi(nb) │
╞══════╪═══════════╡
│ nb_0 │    0.0152 │
├──────┼───────────┤
│ nb_1 │    0.9848 │
╘══════╧═══════════╛
1 loops, best of 1: 815 µs per loop

Exercise: Discuss and code extensions for this model


In [ ]: